<template>
  <div class="home-container">
    <van-nav-bar class="page-nav-bar"
      fixed>
      <van-button class="search-btn"
        slot="title"
        type='info'
        size='small'
        round
        icon='search'
        to='/search'>搜索
      </van-button>
    </van-nav-bar>
    <!-- 频道列表模块 -->
    <van-tabs class="channel-tabs"
      v-model="active"
      animated
      swipeable>
      <van-tab v-for="channel in channels"
        :key="channel.id"
        :title="channel.name">
        <!-- 文章内容模块 -->
        <ArticleList :channel='channel'></ArticleList>
        <!-- 文章内容模块 -->
      </van-tab>
      <div slot="nav-right"
        class="palc"></div>
      <div slot="nav-right"
        class="hamburger-btn"
        @click="isChennelEditShow = true">
        <i class="iconfont icon-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 编辑弹出层 -->
    <van-popup v-model="isChennelEditShow"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '99%' }">
      <ChannelEdit :my-channels='channels'
        :active='active'
        @updata-active='onUpdataActive'>
      </ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  // 组件名称
  name: 'HomeIndex',
  // 局部注册的组件
  components: {
    ArticleList,
    ChannelEdit
  },
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data () {
    return {
      active: 0,
      channels: [],
      isChennelEditShow: false
    }
  },
  // 计算属性
  computed: {
    ...mapState(['user'])
  },
  // 侦听器
  watch: {},
  // 生命周期钩子   注：没用到的钩子请自行删除
  /**
  * 组件实例创建完成，属性已绑定，但DOM还未生成，el属性还不存在
  */
  created () {
    this.loadChannels()
  },
  /**
  * el 被新创建的 vm.el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.el 也在文档内。
  */
  mounted () { },
  // 组件方法
  methods: {
    async loadChannels () {
      try {
        // const { data } = await getUserChannels()
        // this.channels = data.data.channels
        // console.log(this.channels)
        var channels = []
        if (this.user) { // 用户登录获取列表
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else { // 未登录获取的列表
          const localChannels = getItem('TOUTIAO_CHANNELS')
          if (localChannels) {
            channels = localChannels
          } else {
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
      } catch (err) {

      }
      this.channels = channels
    },
    onUpdataActive (ind, isChannelEditShow = true) {
      this.active = ind
      this.isChennelEditShow = isChannelEditShow
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  ::v-deep .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/.channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      z-index: 1;
      top: 92px;
      left: 0;
      right: 0;
      height: 82px;
    }
    .van-tab {
      min-width: 200px;
      border-right: 1px solid #edeff3;
      font-size: 30px;
      color: #777;
    }
    .van-tab--active {
      color: #333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background: #3296fa;
    }
    .palc {
      width: 66px;
      height: 82px;
      flex-shrink: 0;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      opacity: 0.902;
      i.icon {
        font-size: 33px;
      }
      &:before {
        content: "";
        width: 1px;
        position: absolute;
        right: 0;
        height: 100px;
        background-image: url(~@/assets/gradient-gray-line.png);
      }
    }
  }
}
</style>
